package Region.socket;

import Region.DatabaseManager;
import Region.LogManager;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;

public class MasterRegionCopyManager implements Runnable{
    private ServerSocket serverSocket;
    private DatabaseManager dataBaseManager;
    private LogManager logManager;
    private MasterSocketManager masterSocketManager;

    @Override
    public void run() {
        //client核心监听线程,每当监听到client的连接就启动一个clientThread
        System.out.println("主从复制核心监听线程启动成功");
        try {
            while (true) {
                Thread.sleep(1000);
                // 等待与之连接的客户端
                Socket socket = serverSocket.accept();
                // 建立子线程并启动
                CopyThread copyThread = new CopyThread(socket,masterSocketManager,dataBaseManager,logManager);
                Thread thread = new Thread(copyThread);
                thread.start();
            }
        } catch (InterruptedException | IOException e) {
            e.printStackTrace();
        }
    }

    public MasterRegionCopyManager(int port,MasterSocketManager masterSocketManager, DatabaseManager dataBaseManager, LogManager logManager)
            throws IOException, InterruptedException {
        this.serverSocket = new ServerSocket(port);

        this.masterSocketManager = masterSocketManager;
        this.logManager = logManager;
        this.dataBaseManager = dataBaseManager;
    }
}
